<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.xxb.entity.Order"%>
<%@page import="com.xxb.util.DateUtil"%>
<%@page import="com.xxb.entity.User"%>
<%@page import="com.xxb.dao.OrderDao"%>
<%@page import="com.xxb.entity.OrderItem"%>
<%@page import="com.xxb.dao.OrderItemDao"%>
<%@page import="com.xxb.entity.CartItem"%>
<%
	request.setCharacterEncoding("UTF-8");
	
	String name = request.getParameter("name");
	String phone = request.getParameter("phone");
	String address = request.getParameter("address");
	
	Order order = new Order();
	String orderId = DateUtil.getNow();
	
	User user = (User)session.getAttribute("curruser");
	if(user == null){
	
		response.sendRedirect("login.jsp?code=111");
	}else{
		order.setId(orderId);
		order.setAddress(address);
		order.setName(name);
		order.setPhone(phone);
		order.setUid(user.getId());
		order.setState("申核中");
		
		int rows = new OrderDao().save(order);
		if(rows > 0){
			
			List<CartItem> shoplist = (List<CartItem>)session.getAttribute("shopcar");
			for(CartItem item : shoplist){
				
				OrderItem oitem = new OrderItem();
				
				oitem.setOid(orderId);
				oitem.setPid(item.getProduct().getId());
				oitem.setPnum(item.getNum());
				oitem.setTotalprice((item.getTotal()));
				
				int row = new OrderItemDao().save(oitem);
			}
		}
	

		request.setAttribute("order",order);
		request.getRequestDispatcher("ordercheck.jsp").forward(request,response);
	}
%>